﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aweber;

namespace UnitTests
{
    public class MockAdapter: Aweber.IAdapter
    {

        #region Properties

        public String ConsumerKey { get; set; }
        public String ConsumerSecret { get; set; }
        public String AppId { get; set; }

        public String CallbackUrl { get; set; }

        public String OAuthTokenSecret { get; set; }
        public String OAuthToken { get; set; }
        public String OAuthVerifier { get; set; }

        #endregion

        /// <summary>
        /// Connects to the API, issues the command and retrieves the JSON response
        /// </summary>
        /// <param name="url">The base url to contact for the request</param>
        /// <returns>The JSON returned</returns>
        public String GetResponse(String url)
        {
            String response = String.Empty;

            if (url.EndsWith("lists"))
            {
                return "{\"total_size\": 1, \"start\": 0, \"entries\": [{\"total_unconfirmed_subscribers\": 0, \"name\": \"testlist\", \"total_subscribers_subscribed_yesterday\": 0, \"http_etag\": \"ac375192c652b45808318b7a92f092e4187f8620-ca5feee2b7fbb6febfca8af5541541ea960aaedb\", \"web_form_split_tests_collection_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/1947868/web_form_split_tests\", \"subscribers_collection_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/1947868/subscribers\", \"total_subscribers_subscribed_today\": 0, \"id\": 1947868, \"total_subscribed_subscribers\": 1, \"total_unsubscribed_subscribers\": 0, \"campaigns_collection_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/1947868/campaigns\", \"custom_fields_collection_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/1947868/custom_fields\", \"self_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/1947868\", \"total_subscribers\": 1, \"resource_type_link\": \"https://api.aweber.com/1.0/#list\", \"web_forms_collection_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/1947868/web_forms\"}], \"resource_type_link\" : \"https://api.aweber.com/1.0/#list-page-resource\"}";
            }

            if (url.EndsWith("subscribers"))
            {
                return "{\"total_size\": 1, \"start\": 0, \"next_collection_link\": \"\", \"entries\": [{\"subscription_url\": \"http://www.test.com/\", \"postal_code\": \"22309\", \"id\": 48193708, \"custom_fields\": {\"Timezone\": null}, \"last_followup_sent_link\": null, \"city\": \"Alexandria\", \"http_etag\": \"7a463d56a690b3602bc5b9967eaa6a5f74a940e5-e3295d8d02587e7cf232f1f8c2beca41ea5ee58b\", \"ad_tracking\": \"natura_ti\", \"dma_code\": 511, \"last_followup_message_number_sent\": 1001, \"last_followup_sent_at\": null, \"misc_notes\": \"\", \"latitude\": 38.722200000000001, \"is_verified\": true, \"email\": \"shakadanatestmailail.com\", \"status\": \"subscribed\", \"area_code\": 703, \"unsubscribed_at\": null, \"self_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/1936086/subscribers/48193708\", \"unsubscribe_method\": null, \"ip_address\": \"71.163.38.237\", \"name\": \"shaka\", \"subscription_method\": \"signup form\", \"resource_type_link\": \"https://api.aweber.com/1.0/#subscriber\", \"region\": \"VA\", \"longitude\": -77.107399999999998, \"verified_at\": \"2011-08-10 11:39:06-04:00\", \"subscribed_at\": \"2011-08-10 11:32:24-04:00\", \"country\": \"United States\"}], \"resource_type_link\" : \"https://api.aweber.com/1.0/#subscriber-page-resource\"}";
            }

            if (url.EndsWith("campaigns"))
            {
                return "{\"total_size\": 1, \"start\": 0, \"entries\": [{\"id\": 571075, \"http_etag\": \"4d44a6068b43aaa9840d50060bd6087592bdd3f8-ca5feee2b7fbb6febfca8af5541541ea960aaedb\", \"click_tracking_enabled\" : 0, \"total_clicks\" : 0, \"total_opens\" : 0, \"total_sent\" : 0, \"total_spam_complaints\" : 0, \"total_undelivered\" : 0, \"total_unsubscribes\" : 0, \"is_archived\" : 0, \"message_number\" : 0, \"content_type\" : \"test\", \"links_collection_link\" : \"test\", \"messages_collection_link\" : \"test\", \"spam_assassin_score\" : \"test\", \"subject\" : \"test\"}], \"resource_type_link\" : \"https://api.aweber.com/1.0/#campaign-page-resource\"}";
            }

            if (url.EndsWith("accounts"))
            {
                return "{\"total_size\": 1, \"start\": 0, \"entries\": [{\"http_etag\": \"4d44a6068b43aaa9840d50060bd6087592bdd3f8-ca5feee2b7fbb6febfca8af5541541ea960aaedb\", \"lists_collection_link\": \"https://api.aweber.com/1.0/accounts/571075/lists\", \"self_link\": \"https://api.aweber.com/1.0/accounts/571075\", \"resource_type_link\": \"https://api.aweber.com/1.0/#account\", \"id\": 571075, \"integrations_collection_link\": \"https://api.aweber.com/1.0/accounts/571075/integrations\"}], \"resource_type_link\" : \"https://api.aweber.com/1.0/#accounts\"}";
            }

            if (url.Contains("/subscribers/"))
            {
                return "{\"subscription_url\": null, \"postal_code\": null, \"id\": 446105138, \"custom_fields\": {}, \"last_followup_sent_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/2114998/campaigns/f11024235\", \"city\": null, \"http_etag\": \"38dd02a3c1ae5c08c660eba803d70b8afd01cc6e-d6f2b81aa8b62d05f3c94089b001b2705c4c8570\", \"ad_tracking\": \"api\", \"dma_code\": null, \"last_followup_message_number_sent\": 1, \"last_followup_sent_at\": null, \"misc_notes\": null, \"latitude\": null, \"is_verified\": false, \"email\": \"testingz40@testingz30.com\", \"status\": \"unconfirmed\", \"area_code\": null, \"unsubscribed_at\": null, \"self_link\": \"https://api.aweber.com/1.0/accounts/571075/lists/2114998/subscribers/446105138\", \"unsubscribe_method\": null, \"ip_address\": null, \"name\": \"Adam Pedley\", \"subscription_method\": \"api\", \"resource_type_link\": \"https://api.aweber.com/1.0/#subscriber\", \"region\": null, \"longitude\": null, \"verified_at\": null, \"subscribed_at\": \"2012-01-20 09:30:52-05:00\", \"country\": null}";
            }

            return response;
        }

        public Aweber.OAuth.Request BuildRequest()
        {
            // Build Post Data
            Aweber.OAuth.Request request = new Aweber.OAuth.Request();

            // Set Values
            request.oauth_consumer_key = ConsumerKey;
            request.oauth_consumer_secret = ConsumerSecret;
            request.oauth_token = OAuthToken;
            request.oauth_token_secret = OAuthTokenSecret;
            request.oauth_verifier = OAuthVerifier;

            return request;
        }

    }
}
